package com.dst.smart.handler.disposer;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.dst.smart.app.MainActivity;
import com.dst.smart.handler.entity.SemanticResult;
import com.dst.smart.handler.entity.ServiceType;
import com.dst.smart.music.MusicPlayMedia;
import com.dst.smart.player.VolumeManager;
import com.dst.smart.player.entity.SongPlayInfo;
import com.dst.smart.threadpool.ThreadPoolTaskSong;
import com.dst.smart.tools.AppTools;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 其他。
 * 
 * author <a href="http://www.xfyun.cn">讯飞开放平台</a>
 * date 2016年6月21日 下午4:29:54
 *
 */
public class OtherDisposer extends Disposer {

	private final static String OPERATION_INS = "SET";


	private final static String INSTYPE_NEXT = "next";
	private final static String INSTYPE_PAST = "past";
	private final static String INSTYPE_REPLAY = "replay";
	private final static String INSTYPE_REPEAT = "repeat";
	private final static String INSTYPE_PAUSE = "pause";

	private final static String INSTYPE_REPLAYANSWER = "replayAnswer";

	private final static String INSTYPE_VOLMAX = "volume_max";
	private final static String INSTYPE_VOLMID = "volume_mid";
	private final static String INSTYPE_VOLMIN = "volume_min";
	private final static String INSTYPE_VOLMINUS = "volume_minus";
	private final static String INSTYPE_VOLPLUS = "volume_plus";
	private final static String VOLUME_SELECT = "volume_select";

	private VolumeManager mVolumeManager;
	private boolean isIncrease = false;
	private boolean PlaySource = false;
	enum INSTYPE {
		MAX, MIN
	};

	private static Map<String, INSTYPE> insTypeMap;

	static {
		insTypeMap = new HashMap<String, INSTYPE>();
		insTypeMap.put("MAX", INSTYPE.MAX);
		insTypeMap.put("MIN", INSTYPE.MIN);
	}
	private int currentVolume = -1;
	public OtherDisposer(Context context) {
		super(context);
		mVolumeManager=VolumeManager.getInstance();
	}

	@Override
	public void disposeResult(final SemanticResult result) {
		try {
			Log.e("text","Other"+result.getAnswerText());
			String operation = result.getOperation();
			if (OPERATION_INS.equals(operation)) {
				JSONObject semantic = result.getSemantic();
				String attrType = semantic.getJSONObject(KEY_SLOTS).getString("attrType");
				if (attrType.equals("String")) {
					String attrValue = semantic.getJSONObject(KEY_SLOTS).getString("attrValue");
					INSTYPE type = insTypeMap.get(attrValue);
					switch (type) {
						case MAX:
							if(MainActivity.musicPlaySt) {
								if (MusicPlayMedia.getInstansMedia() != null) {
									MusicPlayMedia.getInstansMedia().setPlayerVolume("100");
									MusicPlayMedia.getInstansMedia().setVolume();
								}
							}else {
								mVolumeManager.setMaxVolume();
							}
							break;
						case MIN:
							if(MainActivity.musicPlaySt) {
								if (MusicPlayMedia.getInstansMedia() != null) {
									MusicPlayMedia.getInstansMedia().setPlayerVolume("0");
									MusicPlayMedia.getInstansMedia().setVolume();
								}
							}else{
								mVolumeManager.setMinVolume();
							}
							break;
					}
				} else if ("Integer".equals(attrType)) {
					String attrValue = semantic.getJSONObject(KEY_SLOTS).getString("attrValue");
					if(AppTools.isNumeric(attrValue)){
						if(MainActivity.musicPlaySt){
						if (MusicPlayMedia.getInstansMedia() != null) {
							MusicPlayMedia.getInstansMedia().setPlayerVolume(attrValue);
							MusicPlayMedia.getInstansMedia().setVolume();
						}
						}else{
							mVolumeManager.setVolume(Integer.parseInt(attrValue));
						}

					}
				}
			}else{
				String text = result.getAnswerText();
				if(!TextUtils.isEmpty(text)){
					getPlayController().playURLList(result.getUUID(),null);
					getPlayController().playText(result.getUUID(), text);
				}
			}
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
	}
	
	public boolean canHandle(ServiceType type){
		return (type == ServiceType.OTHER);
	}

	private void parseMusicJson(SemanticResult semanticResult) {

		try {
			JSONArray result = semanticResult.getResult();
			
			if (null != result && result.length() > 0) {
				for (int i = 0; i < result.length(); i++) {

					JSONObject songInfoJson = result.getJSONObject(i);
					String singernames = songInfoJson.getString("singernames");

						ThreadPoolTaskSong songThread = new ThreadPoolTaskSong(	singernames,
								singernames, "music",
								songInfoJson.optString("audiopath"),
								songInfoJson.optString("songname"));
						MainActivity.activity.poolManager
								.addAsyncTask(songThread);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	
}
